function [ cst, coloring, csts ] = simAnn(graph, startingColoring, ...
                                     initialTemperature, coolingRate, ...
                                     maxIterations )
%SIMANN Symulowane wyzarzanie
%   graph - graf
%   startingColoring - początkowe pomalowanie
%   initialTemperature - początkowa temperatura
%   coolingRate - wspolczynnik schladzania
%   maxIterations - maksymalna liczba iteracji

    it = 1;
    temperature = initialTemperature;
    oldCost = cost(graph, startingColoring);
    coloring = startingColoring;
    csts(1) = oldCost;

    while it < maxIterations
        tmpColoring = neighbourColoring(coloring);
        currCost = cost(graph, tmpColoring);
        diff = abs(currCost - oldCost);
        if currCost < oldCost
            coloring = tmpColoring;
            temperature = coolingRate * temperature;
            oldCost = currCost;
            it = it + 1;
        else %currCost >= oldCost
            if rand(1) < exp(-diff/temperature)
                coloring = tmpColoring;
                temperature = coolingRate * temperature;
                oldCost = currCost;
                it = it + 1;
            end
        end %end if currCost < oldCost
        csts(it) = oldCost;
    end %end while
cst = cost(graph, coloring);
end
